package com.ruoyi.web.controller.data;

import com.ruoyi.base.domain.BaseLimit;
import com.ruoyi.base.service.IBaseLimitService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SystemType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.domain.DataGold;
import com.ruoyi.data.service.IDataGoldService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 金豆记录Controller
 *
 * @author zzp
 * @date 2022-09-30
 */
@RestController
@RequestMapping("/data/gold")
public class DataGoldController extends BaseController {

    @Autowired
    private IDataGoldService dataGoldService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IBaseLimitService limitService;

    /**
     * 查询金豆记录列表
     */
    @PreAuthorize("@ss.hasPermi('data:gold:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataGold dataGold) {
        startPage();
        List<DataGold> list = dataGoldService.selectDataGoldList(dataGold);
        return getDataTable(list);
    }

    /**
     * 导出金豆记录列表
     */
    @PreAuthorize("@ss.hasPermi('data:gold:export')")
    @Log(title = "金豆记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataGold dataGold) {
        List<DataGold> list = dataGoldService.selectDataGoldList(dataGold);
        ExcelUtil<DataGold> util = new ExcelUtil<DataGold>(DataGold.class);
        util.exportExcel(response, list, "金豆记录数据");
    }

    /**
     * 获取金豆记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:gold:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(dataGoldService.selectDataGoldById(id));
    }

    /**
     * 新增金豆记录
     */
    @PreAuthorize("@ss.hasPermi('data:gold:add')")
    @Log(title = "金豆记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataGold dataGold) {
        if (StringUtils.isNull(dataGold.getUserId())) {
            return AjaxResult.error("请选择用户");
        } else {
            SysUser user = userService.selectUserById(dataGold.getUserId());
            if (StringUtils.isNull(user)) {
                return AjaxResult.error("用户不存在");
            }
            if (dataGold.getGoldType().equals(SystemType.IN.getCode())) {
                user.setGoldAvailable(user.getGoldAvailable() + dataGold.getGold());
            } else {
                if (user.getGoldAvailable() < dataGold.getGold()) {
                    return AjaxResult.error("用户可用金豆不足");
                }
                user.setGoldAvailable(user.getGoldAvailable() - dataGold.getGold());
            }
            BaseLimit limit = limitService.selectBaseLimitUse();
            int userAvailable = user.getGoldAvailable() / Integer.parseInt(limit.getGoldScale());
            user.setUserAvailable(userAvailable);
            userService.updateUserProfile(user);
            dataGold.setGoldCurrent(user.getGoldAvailable());
            dataGoldService.insertDataGold(dataGold);
            return AjaxResult.success();
        }
    }

    /**
     * 修改金豆记录
     */
    @PreAuthorize("@ss.hasPermi('data:gold:edit')")
    @Log(title = "金豆记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataGold dataGold) {
        return toAjax(dataGoldService.updateDataGold(dataGold));
    }

    /**
     * 删除金豆记录
     */
    @PreAuthorize("@ss.hasPermi('data:gold:remove')")
    @Log(title = "金豆记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(dataGoldService.deleteDataGoldByIds(ids));
    }
}
